//
// Inter-Widget drag and drop extension to the Widget object
// that is accessible from within a widget iframe in a Portal
//
// This can be run also in a non-portal context, in that case
// it creates the Widget class and widget object
//
// TO DO
// - properly handle removeDragData

// Extends the Widget class with new methods
Widget.prototype.bindWidgetToDropType = function (type)
{
	if (! this.registeredWithDragManager) {
		parent.dragManager.registerWidget (this.id, this);
		this.registeredWithDragManager = true;
	}
	this.dragManager.bindWidgetToDropType (this.id, type);
}

Widget.prototype.addDragData = function (target, dragType, dragData, tooltip, protect)
{
	// FIXE: currently we have to register the widget event if it isn't bound to drop event
	// this is because DragManager.startDrag uses the widget position to compute initial ghost position
	if (! this.registeredWithDragManager) {
		this.dragManager.registerWidget (this.id, this);
		this.registeredWithDragManager = true;
	}

	// closure for callback (shouldn't  we create a special DragData object for all this ?)
	var mgr = this;
	var type = dragType;
	var data = dragData;
	var message = tooltip; //  memorizes the message to display while dragging
	var dragDetected = function (ev) {
		return mgr._doDragDetected(ev, type, data, message);
	}

	// To prevent the mousedowm effect due to the drag event
	if(protect != undefined){
		$(protect, target).bind('mousedown', function(){return false});
	}

	target.onselectstart = function () {return false;};		// IE Hack to prevent text selection.

	// installs an event handler
	$(target).bind('mousedown', dragDetected);
}

Widget.prototype.removeDragData = function (target)
{
	// TBD: we should memorize the event handler for this target to remove just it
	target.removeEventListener ('mousedown');
}

// passes control to the Portal drag manager component
Widget.prototype._doDragDetected = function (ev, type, data, message)
{
	this.dragManager.startDrag(ev.clientX, ev.clientY, this.id, type, data, message);
	if (ev.preventDefault)
		ev.preventDefault();

	if (ev.stopPropagation)
		ev.stopPropagation();


}